package com.tongqu.Test;

import com.tongqu.entity.Login;
import com.tongqu.message.LoginMessage;
import com.tongqu.message.Message;
import com.tongqu.protocol.MessageCodec;
import com.tongqu.protocol.TestCodec;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;


@Slf4j
public class ChatClient {
    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.INFO);
        TestCodec MESSAGE_CODEC = new TestCodec();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(group);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(LOGGING_HANDLER);
                    ch.pipeline().addLast(MESSAGE_CODEC);
                    ch.pipeline().addLast("clienthanler",new ChannelInboundHandlerAdapter(){
                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            log.debug("{}",msg);
                            super.channelRead(ctx, msg);
                        }

                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            new Thread(()->{
                                while (true){
                                    System.out.println("username");
                                    Scanner scanner = new Scanner(System.in);
                                    String name = scanner.nextLine();
                                    Scanner pass = new Scanner(System.in);
                                    String password = scanner.nextLine();
                                    Login acct = new Login(name, password);
                                    LoginMessage user = new LoginMessage(1,acct);
                                    ctx.writeAndFlush(user);
                                }


                            },"SYSTEM IN").start();

                        }
                    });
                }
            });
            Channel channel = bootstrap.connect("localhost", 8080).sync().channel();
            channel.closeFuture().sync();
        } catch (Exception e) {
            log.error("client error", e);
        } finally {
            group.shutdownGracefully();
        }
    }
}